package main.users;

import main.SQL.MsSQLConnect;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FactageUser {
    private Integer id, roleId;
    private String login, name, roleName, password;
    private Connection connSQL;
    private Boolean newUser = false;

    public FactageUser(){

    }

    //region FactageUser(Integer id, String login, String name, Integer roleId, String roleName) - конструктор для создания объекта пользователь при выгрузке таблицы пользователей
    public FactageUser(Integer id, String login, String name, Integer roleId, String roleName){
        this.id = id;
        this.login = login;
        this.name = name;
        this.roleId = roleId;
        this.roleName = roleName;
    }
    //endregion

    //region FactageUser(String login, String password) - конструктор для создания объекта пользователь при логине
    public FactageUser(String login, String password){
        this.login = login;

        try {

            connSQL = MsSQLConnect.ConnectDB("Factage");

            Statement stat = connSQL.createStatement();

//            stat.executeQuery( "UPDATE users SET password = '"+md5(password)+"' WHERE login = 'admin'" );

            ResultSet rs = stat.executeQuery( "SELECT [id] ,[name] ,[role_id] ,(select name from user_roles where id = users.role_id) as role_name FROM users WHERE login = '"+login+"' and password = '"+md5(password)+"'" );

            while (rs.next()) {
                this.name = rs.getString("name");
                this.id = rs.getInt("id");
                this.roleId = rs.getInt("role_id");
                this.roleName = rs.getString("role_name");
            }

            rs.close();
            stat.close();

            connSQL.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

        if (login.equals(password)) newUser = true;
    }
    //endregion

    //region md5(String input) - расчет хэш-кода пароля
    public static String md5(String input) {

        String md5 = null;
        if(null == input) return null;
        try {

            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(input.getBytes(), 0, input.length());
            md5 = new BigInteger(1, digest.digest()).toString(16);

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return md5;
    }
    //endregion

    //region updatePassword(String password) - обновление пароля в SQL
    public void updatePassword(String password) {
        try {
            connSQL = MsSQLConnect.ConnectDB("Factage");

            Statement stat = connSQL.createStatement();
            stat.executeUpdate("UPDATE users SET [password] = '" + md5(password) + "' WHERE [login] = '" + login + "' ");

            stat.close();

            connSQL.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
    //endregion в SQL

    //region Геттеры и сеттеры для класса
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public Integer getRoleId() {
        return roleId;
    }

    public void setRoleId(int roleId) {
        this.roleId = roleId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public Boolean getNewUser() {
        return newUser;
    }
    //endregion
}
